﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using MM.Entities;
using MM.Business;

using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace MM
{
    public partial class frmBaoCaoTonKho : Form
    {
        string _matau = "";
        string _nguoitao = "";
        string _sConn = "";
        string _plant = "";

        public List<VAT_TU_PHU_TUNG> lstVTPT = new List<VAT_TU_PHU_TUNG>();
        frmTimKiemVTPT frm;
        frmTimKiemTheoHinh frm1;
        frmImpa_TimKiem frm2;
        frmImpa_TimKiemTheoHinh frm3;
       

        public frmBaoCaoTonKho(string sConn)
        {
            InitializeComponent();
            _sConn = sConn;
            _matau = frmDangNhap.objTau._tenTau2KiTu;
            this.InitDMKho(_matau);
            this.InitTrangthai();
            this.InitNhomVTPT();
        }

        private void InitDMKho(string matau)
        {
            clsKHO khoCtrl = new clsKHO(_sConn);
            List<KHO> lstKho = new List<KHO>();
            lstKho = khoCtrl.LoadKHOByTAU(matau);
            this.lookUpKho.Properties.DataSource = lstKho;
            this.lookUpKho.Properties.DisplayMember = "MaKho";
            this.lookUpKho.Properties.ValueMember = "MaKho";
        }
        private void InitTrangthai()
        {
            clsTRANG_THAI_VTPT ttCtrl = new clsTRANG_THAI_VTPT(_sConn);
            List<TRANG_THAI_VTPT> lst = new List<TRANG_THAI_VTPT>();
            lst = ttCtrl.usp_SelectTRANG_THAI_VTPTsAll();
            this.lookUpTrangthai.Properties.DataSource = lst;
            this.lookUpTrangthai.Properties.DisplayMember = "TenTrangThai";
            this.lookUpTrangthai.Properties.ValueMember = "MaTrangThai";
            if (lst.Count > 0)
            {
                this.lookUpTrangthai.Properties.ForceInitialize();
                this.lookUpTrangthai.ItemIndex = 0;
            }
        }

        private void InitNhomVTPT()
        {
            clsNHOM_VTPT nhomCtrl = new clsNHOM_VTPT(_sConn);
            List<NHOM_VTPT> lst = new List<NHOM_VTPT>();
            lst = nhomCtrl.usp_SelectNHOM_VTPT();
            this.lookUpNhomVTPT.Properties.DataSource = lst;
            this.lookUpNhomVTPT.Properties.DisplayMember = "TenNhomVTPT";
            this.lookUpNhomVTPT.Properties.ValueMember = "MaNhomVTPT";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string makho="";
            int matrangthai=-1;
            string manhomVTPT="";
            CHI_TIET_KHO obj;
            try
            {
                makho = this.lookUpKho.GetColumnValue("MaKho").ToString();
            }
            catch { }
            try
            {
                matrangthai = int.Parse(this.lookUpTrangthai.GetColumnValue("MaTrangThai").ToString());
            }
            catch { }
            try
            {
                 manhomVTPT = this.lookUpNhomVTPT.GetColumnValue("MaNhomVTPT").ToString();
            }
            catch { }

            clsCHI_TIET_KHO ctkhoCtrl = new clsCHI_TIET_KHO(_sConn);
            List<CHI_TIET_KHO> lst = new List<CHI_TIET_KHO>();
            List<CHI_TIET_KHO> lstTonKho = new List<CHI_TIET_KHO>();

            if (lstVTPT.Count == 0)
            {
                if (this.txtMaVTPT.Text.Trim() != "")
                {
                    lstVTPT.Add(new VAT_TU_PHU_TUNG());
                    lstVTPT[0]._maVTPT = this.txtMaVTPT.Text.Trim();
                }
                else
                {
                    obj = new CHI_TIET_KHO();
                    obj.MaKho = makho;
                    obj.MaTrangThai = matrangthai;
                    obj.MaNhomVTPT = manhomVTPT;

                    lstTonKho = ctkhoCtrl.usp_SelectCHI_TIET_KHO(obj);
                }
            }

            for (int i = 0; i < lstVTPT.Count; i++)
            {
                lst.Clear();
                obj = new CHI_TIET_KHO();
                obj.MaKho = makho;
                obj.MaTrangThai = matrangthai;
                obj.MaNhomVTPT = manhomVTPT;
                obj.MaThietBi = lstVTPT[i]._maVTPT;

                lst = ctkhoCtrl.usp_SelectCHI_TIET_KHO(obj);
                for (int j = 0; j < lst.Count; j++)
                {
                    lstTonKho.Add(lst[j]);
                }
            }

            if (this.chkThietYeu.Checked == true)
            {
                lstTonKho.RemoveAll(delegate(CHI_TIET_KHO x) { return x.ThietYeu == 0; });
            }

            this.gridControl1.DataSource = lstTonKho;

        }

      

        private void LuuDSVTPT(object sender, lstVtptEventArgs e)
        {
            lstVTPT = e.lstVtpt;
            if (lstVTPT.Count > 0)
            {
                this.txtMaVTPT.Text = lstVTPT[0]._maVTPT;
                this.lblTenVTPT.Text = lstVTPT[0]._tenVTPT;
            }
        }

        private void frmBaoCaoTonKho_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F8)
            {
                this.button1_Click(null, null);
            }
        }

        private void frmBaoCaoTonKho_Load(object sender, EventArgs e)
        {
            Common.SetColor(this);
        }

        private void tìmTheoNhiềuTiêuChíToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstVTPT.Count == 0)
            {
                frm = new frmTimKiemVTPT(_sConn, true);
                frm.SelectLstMaterial += new frmTimKiemVTPT.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm.ShowDialog();
            
        }

        private void tìmTheoHìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstVTPT.Count == 0)
            {
                frm1 = new frmTimKiemTheoHinh(_sConn, true);
                frm1.SelectLstMaterialByImg += new frmTimKiemTheoHinh.SelectListMaterialByImgHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm1.ShowDialog();
        }

        private void tìmVTIMPATheoNhiềuTiêuChíToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm2 == null)
            {
                frm2 = new frmImpa_TimKiem(_sConn, true);
                frm2.SelectLstMaterial += new frmImpa_TimKiem.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm2.ShowDialog();
        }

        private void tìmVTIMPATheoHìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm3 == null)
            {
                frm3 = new frmImpa_TimKiemTheoHinh(_sConn, true);
                frm3.SelectLstMaterial += new frmImpa_TimKiemTheoHinh.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm3.ShowDialog();
        }

        private void chkThietYeu_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                List<CHI_TIET_KHO> lstTonKho = new List<CHI_TIET_KHO>();
                lstTonKho = (List<CHI_TIET_KHO>)this.gridControl1.DataSource;

                if (chkThietYeu.Checked == true)
                {
                  
                    if (lstTonKho != null && lstTonKho.Count > 0)
                    {
                        lstTonKho.RemoveAll(delegate(CHI_TIET_KHO x) { return x.ThietYeu == 0; });

                        this.gridControl1.DataSource = lstTonKho;
                        this.gridControl1.RefreshDataSource();
                    }
                }
                
            }
            catch { }
        }

        private void txtMaVTPT_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                // kiểm tra tồn tại của VTPT                   

                VAT_TU_PHU_TUNG obj = new VAT_TU_PHU_TUNG();

                clsVAT_TU_PHU_TUNG vtptCtrl = new clsVAT_TU_PHU_TUNG(_sConn);

                obj = vtptCtrl.SelectVAT_TU_PHU_TUNG(this.txtMaVTPT.Text.Trim(), frmDangNhap.objTau.maTau);
                if (obj != null && obj._maVTPT != "" && obj._maVTPT != null)
                {
                    this.lblTenVTPT.Text = obj._tenVTPT;
                    
                }
                else
                {
                    MessageBox.Show("KHÔNG TÌM THẤY THÔNG TIN VẬT TƯ NÀY");
                    
                }

            }
        }

        private void gridDSVT_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            GridView View = sender as GridView;
            if (e.RowHandle >= 0)
            {

                string ThietYeu = View.GetRowCellDisplayText(e.RowHandle, "ThietYeu").ToString().Trim();

                if (ThietYeu == "1")
                {
                    e.Appearance.ForeColor = Color.Red;
                    gridDSVT.OptionsSelection.EnableAppearanceFocusedRow = false;
                }
            }
        }
    }
}